﻿using System;
using System.Text;
using System.Threading;
using BrukeFramework;
using RabbitMQ;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace RabbitMQReceivedConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { };
            //factory.HostName = "192.168.3.112";
            factory.HostName = "47.112.201.220";//85-->5672
            factory.UserName = "bruke";
            factory.Password = "123456";
            factory.VirtualHost = "my_vhost";
            factory.Endpoint = new AmqpTcpEndpoint("47.112.201.220", 85);//因为远程的服务器端口是85，不是默认5672

            var channelQueueName = "BrukeQueue";

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(channelQueueName, false, false, false, null);//定义队列
                                                                                      //创建消费者
                    var consumer = new EventingBasicConsumer(channel);//消费者
                    channel.BasicConsume(channelQueueName, true, consumer);//消费消息
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body.ToArray());
                        Console.WriteLine($"接收消息：{message}", ConsoleColor.DarkBlue);
                    };
                    Console.ReadLine();
                }
            }

        }
    }
}
